package com.lin.customEnum;

import com.lin.exception.UnknownRecordStatusException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;

@AllArgsConstructor
@Getter
public enum RecordStatus {
    EXPIRED(-1, "借阅已过期"),
    NOT_EXPIRED(0, "借阅未过期"),
    RETURNED(1, "已还书"),
    LOST(2, "未知");

    private final int status;
    private final String description;

    // 根据状态获取对应枚举实例
    public static String fromCode(int status) {
        return Arrays.stream(RecordStatus.values())
                .filter(recordStatus -> recordStatus.getStatus() == status)
                .findFirst()
                .map(RecordStatus::getDescription)
                .orElseThrow(() -> new UnknownRecordStatusException("unknown record status: " + status));
        /*for (RecordStatus recordStatus : RecordStatus.values()) {
            if (recordStatus.getStatus() == status) {
                return recordStatus;
            }
        }
        throw new UnknownRecordStatusException("unknown record status: " + status);*/
    }
}
